﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Gift.WebApp;
using Gift.Model;
using Gift.DAL;

namespace WebApp.admin.news
{
    public partial class articleType : BaseAdmin
    {
        // Fields
        tbArticleType oArticleType = new tbArticleType();

        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                this.oArticleType.typeName = this.txtTypeName.Value;
                this.oArticleType.sortOrder = Convert.ToInt32(this.txtSortOrder.Value);
                this.oArticleType.keywords = this.txtKeywords.Value;
                this.oArticleType.description = this.txtaDescription.Value;
                this.oArticleType.isShow = Convert.ToBoolean(base.Request.Form["isShow"]);
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oArticleType.id = Guid.Parse(base.Request.QueryString["id"]);
                    DataTable articleTypeById = BaseFactory.theArticleType.GetArticleTypeById(this.oArticleType.id.ToString());
                    this.oArticleType.parentId = Guid.Parse(articleTypeById.Rows[0]["parentId"].ToString());
                    if (BaseFactory.theArticleType.Update(this.oArticleType))
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"articleTypeList.aspx?pid=" + this.oArticleType.parentId + "\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                    }
                }
                else if ((base.Request.QueryString["ot"] == "ad") && (base.Request.QueryString["pid"] != null))
                {
                    this.oArticleType.id = Guid.NewGuid();
                    this.oArticleType.parentId = Guid.Parse(base.Request.QueryString["pid"]);
                 
                    if (BaseFactory.theArticleType.Add(this.oArticleType) > 0)
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"articleTypeList.aspx?pid=" + this.oArticleType.parentId + "\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                    }
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
        }

        public StringBuilder GetBcakLocation()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if (base.Request.QueryString["id"] != null)
                {
                    DataTable articleTypeById = BaseFactory.theArticleType.GetArticleTypeById(base.Request.QueryString["id"]);
                    if (articleTypeById.Rows.Count > 0)
                    {
                        builder.Append("location.href='articleTypeList.aspx?pid=" + articleTypeById.Rows[0]["parentId"].ToString() + "';");
                    }
                    return builder;
                }
                if (base.Request.QueryString["pid"] != null)
                {
                    builder.Append("location.href='articleTypeList.aspx?pid=" + base.Request.QueryString["pid"] + "';");
                }
            }
            catch
            {
            }
            return builder;
        }

        public StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null))
                {
                    builder.Append("编辑分类");
                    return builder;
                }
                if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["pid"] != null))
                {
                    builder.Append("添加分类");
                }
            }
            catch
            {
            }
            return builder;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.ArticleClass);
                if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null))
                {
                    this.RegionBind();
                }
                else if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["pid"] != null))
                {
                    if (base.Request.QueryString["pid"] == "00000000-0000-0000-0000-000000000000")
                    {
                        this.lblParentType.InnerText = "顶级分类";
                    }
                    else
                    {
                        DataTable articleTypeById = BaseFactory.theArticleType.GetArticleTypeById(base.Request.QueryString["pid"]);
                        if (articleTypeById.Rows.Count > 0)
                        {
                            this.lblParentType.InnerText = articleTypeById.Rows[0]["typeName"].ToString();
                        }
                    }
                    this.rdIsShow1.Checked = true;
                }
            }
        }

        private void RegionBind()
        {
            try
            {
                DataTable articleTypeById = BaseFactory.theArticleType.GetArticleTypeById(base.Request.QueryString["id"]);
                if (articleTypeById.Rows.Count > 0)
                {
                    if (articleTypeById.Rows[0]["parentId"].ToString() != "00000000-0000-0000-0000-000000000000")
                    {
                        DataTable table2 = BaseFactory.theArticleType.GetArticleTypeById(articleTypeById.Rows[0]["parentId"].ToString());
                        if (table2.Rows.Count > 0)
                        {
                            this.lblParentType.InnerText = table2.Rows[0]["typeName"].ToString();
                        }
                    }
                    else
                    {
                        this.lblParentType.InnerText = "顶级分类";
                    }
                    this.txtTypeName.Value = articleTypeById.Rows[0]["typeName"].ToString();
                    this.txtSortOrder.Value = articleTypeById.Rows[0]["sortOrder"].ToString();
                    if (Convert.ToInt32(articleTypeById.Rows[0]["isShow"]) == 1)
                    {
                        this.rdIsShow1.Checked = true;
                    }
                    else
                    {
                        this.rdIsShow0.Checked = true;
                    }
                    this.txtKeywords.Value = articleTypeById.Rows[0]["keywords"].ToString();
                    this.txtaDescription.Value = articleTypeById.Rows[0]["description"].ToString();
                }
            }
            catch
            {
            }
        }

    }
}